package util;

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

import model.*;

public class DownloadServlet extends HttpServlet {
	
	private String saveFilePath;
	
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//%����� ������ ����� ���� ��ξ��
		if(saveFilePath ==null){
			ServletContext sc = request.getSession().getServletContext();
			saveFilePath = sc.getRealPath("/savefile");
		}		
		
		// Ŭ���̾�Ʈ�κ��� ���� �ĺ��� ���
		String fno = request.getParameter("fno");
		String bno = request.getParameter("bno");

		// fileupload table�� ����� ������ d���� �� �ٿ�ε� ��Ŵ
		if (fno != null) {
			readFileUploadTable(response, fno);
		}
		//board ���̺? ����� ������ d���� �� �ٿ�ε� ��Ŵ
		if(bno!=null){
			readBoardTable(response, bno);
		}
	}
	
	
	
	public void readBoardTable(HttpServletResponse response, String bno){
		Connection conn = null;
		try {
			// ���̺?�� ������ �� �̸�, ������ �����̸�, ������ Ÿ�� ���
			Class.forName("oracle.jdbc.OracleDriver");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:mydb", "scott",
					"tiger");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt
					.executeQuery("select originalname, savedname, filetype from board where bno="
							+ bno);
	
			if (rs.next()) {
				String originalName = rs.getString("originalname");
				String savedName = rs.getString("savedname");
				String fileType = rs.getString("filetype");
				
				sendFile(response, originalName, savedName, fileType);
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}



	private void readFileUploadTable(HttpServletResponse response, String fno){
		Connection conn = null;
		try {
			// ���̺?�� ������ �� �̸�, ������ �����̸�, ������ Ÿ�� ���
			conn = ConnectionManager.getConnection();
			
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt
					.executeQuery("select * from fileupload where fno="
							+ fno);

			if (rs.next()) {
				String originalName = rs.getString("originalname");
				String savedName = rs.getString("savedname");
				String fileType = rs.getString("filetype");
				
				sendFile(response, originalName, savedName, fileType);
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}
	
	private void sendFile(HttpServletResponse response, String originalName,
			String savedName, String fileType) {
		//(1)Ŭ���̾�Ʈ�� ����; �ٿ�ε��ؾ� ��; �4� HTTP��� ����		
		try {
			//http���� �ѱ��� ���Ե� �� ��� ������ 8859_1�� ���ڵ��ؾ� ��
			originalName = new String(originalName.getBytes("utf-8"),"8859_1");
		} catch (UnsupportedEncodingException e) {			
			e.printStackTrace();
		}
		response.setHeader("Content-Disposition", "attachment;filename="+originalName);
		
		//(2)������ Ÿ��; �4� HTTP��� ����
		response.setContentType(fileType);
		
		//(3)���� �����͸� �4� Http �ٵ� ����
		try {
			//�4�Http�ٵ�� ����ϴ� ��½�Ʈ�� ��� (���̳ʸ� ��Ʈ��)
			ServletOutputStream sos = response.getOutputStream();
			//���Ϸκ��� �����͸� �д� �Է½�Ʈ�� ���(���̳ʸ� ��Ʈ��)
			FileInputStream fis = new FileInputStream(saveFilePath+"/"+savedName);
			//������ �����͸� �а� ����ϱ�
			byte[] datas = new byte[1024];
			int n = -1;
			while((n=fis.read(datas,0,1024))!=-1){
				sos.write(datas,0,n);
			}
			fis.close();
			sos.close();
			
		} catch (IOException e) {		
			e.printStackTrace();
		}
		
	}

}


















